package com.anhtuan.reminder.listelement;

import com.anhtuan.reminder.MainActivity;
import com.anhtuan.reminder.R;
import com.anhtuan.reminder.ReminderApplication;
import com.anhtuan.reminder.SelectionActivity;
import com.anhtuan.reminder.providers.TaskItem;
import com.anhtuan.reminder.ultils.Ultils;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class NewTaskListElement extends BaseListElement{

	public NewTaskListElement(Activity activity, int requestCode) {
		super(activity,
				activity.getResources().getDrawable(R.drawable.action_add), 
				activity.getResources().getString(R.string.newtask_text),
				activity.getResources().getString(R.string.newtask_description), 
				null, 
				requestCode);
	}

	@Override
	public OnClickListener getOnClickListener() {
		return new OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent intent = new Intent();
				intent.setData(SelectionActivity.NEW_ACTION);
				intent.setClass(getActivity(), SelectionActivity.class);
				getActivity().startActivityForResult(intent, getRequestCode());
			}
		};
	}

	@Override
	public void onActivityResult(Intent data) {

		MainActivity mainActivity = (MainActivity) getActivity();
		ReminderApplication app = (ReminderApplication) mainActivity.getApplication();
		TaskItem taskItem = app.getTaskItem();
		
		BaseListElement element = null;
		
		if (taskItem != null) {
			element = Ultils.convertToBaseListElement(getActivity(), taskItem, mainActivity.getListElementsSize());
		}
		
		if (element != null) {
			mainActivity.startScheduleATask(taskItem);
			mainActivity.addTask(element);
		}
	}
}
